package test.data_structure.stack;

public class Solution {

    public static boolean isValid(String s) {
        if (null == s || s.length() == 0) {
            return false;
        }
        MyArrayStack<Character> stack = new MyArrayStack<>();
        for (int i = 0 ; i < s.length() ; i ++) {
            char c = s.charAt(i);
            if (c == '{' || c == '[' || c == '(') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                char topChar = stack.pop();
                if (c == '}' && topChar != '{') {
                    return false;
                } else if (c == ']' && topChar != '[') {
                    return false;
                } else if (c == ')' && topChar != '(') {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(isValid("[{(])}"));
        System.out.println(isValid("[{()}]"));
        System.out.println(isValid("[]{}()"));
    }

}
